﻿using System;
using System.Collections.Generic;

namespace SBPweb.MediaCenter.Common.Smartparts
{

    public abstract class MenuBase<T> : List<T>, IMenuList where T : IMenuItem
    {

        #region Constructor logic

        protected MenuBase()
        {
        }

        #endregion


        #region IMenuList Members

        public IMenuItem[] Items
        {
            get
            {
                return Array.ConvertAll<T, IMenuItem>(base.ToArray(), new Converter<T, IMenuItem>(ConvertTToIMenuItem));
            }
        }

        public event EventHandler<MenuEventArgs> ItemAdded;

        public event EventHandler<MenuEventArgs> ItemRemoved;

        #endregion

        #region Helper methods

        public static IMenuItem ConvertTToIMenuItem(T t)
        {
            return (IMenuItem)t;
        }

        #endregion


        #region IList<IMenuItem> Members

        public int IndexOf(IMenuItem item)
        {
            return base.IndexOf((T)item);
        }

        public void Insert(int index, IMenuItem item)
        {
            base.Insert(index, (T)item);
            if (ItemAdded != null)
            {
                ItemAdded.Invoke(this, new MenuEventArgs(item));
            }
        }

        public new IMenuItem this[int index]
        {
            get
            {
                return (IMenuItem)base[index];
            }
            set
            {
                base[index] = (T)value;
            }
        }

        #endregion

        #region ICollection<IMenuItem> Members

        public void Add(IMenuItem item)
        {
            base.Add((T)item);
            if (ItemAdded != null)
            {
                ItemAdded.Invoke(this, new MenuEventArgs(item));
            }
        }

        public bool Contains(IMenuItem item)
        {
            return base.Contains((T)item);
        }

        public void CopyTo(IMenuItem[] array, int arrayIndex)
        {
            throw new NotImplementedException();
        }

        public bool IsReadOnly
        {
            get
            {
                return false;
            }
        }

        public bool Remove(IMenuItem item)
        {
            bool ret = base.Remove((T)item);
            if (ItemRemoved != null)
            {
                ItemRemoved.Invoke(this, new MenuEventArgs(item));
            }
            return ret;
        }

        #endregion

        #region IEnumerable<IMenuItem> Members

        public new IEnumerator<IMenuItem> GetEnumerator()
        {
            throw new NotImplementedException();
        }

        #endregion
    }

}